<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_lap_peserta extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'd.kd_prodi';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$PMBPeriodID = isset($_POST['PMBPeriodID']) ? strval($_POST['PMBPeriodID']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.asal_sekolah, a.no_telp, a.email ');
			$this->db->select('b.nama_fakultas as fakultas1,d.nama_prodi as prodi1, c.nama_fakultas as fakultas2, e.nama_prodi as prodi2, a.status');
			$this->db->from('pmb_pendaftaran a');
			$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.fakultas1', 'left');
			$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.fakultas2', 'left');
			$this->db->join('ref_prodi d', 'd.kd_prodi = a.prodi1', 'left');
			$this->db->join('ref_prodi e', 'e.kd_prodi = a.prodi2', 'left');
			
			if($PMBPeriodID != ''){
				$this->db->where('a.PMBPeriodID', $PMBPeriodID);
			}
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	//PDF
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
		$this->db->select('a.id_pendaftaran, a.PMBPeriodID, a.no_pendaftaran, a.nama, a.alamat, a.asal_sekolah, a.no_telp, a.email ');
		$this->db->select('b.nama_fakultas as fakultas1,d.nama_prodi as prodi1, c.nama_fakultas as fakultas2, e.nama_prodi as prodi2, a.status');
		$this->db->from('pmb_pendaftaran a');
		$this->db->join('ref_fakultas b', 'b.kd_fakultas = a.fakultas1', 'left');
		$this->db->join('ref_fakultas c', 'c.kd_fakultas = a.fakultas2', 'left');
		$this->db->join('ref_prodi d', 'd.kd_prodi = a.prodi1', 'left');
		$this->db->join('ref_prodi e', 'e.kd_prodi = a.prodi2', 'left');
					
		$this->db->where('a.PMBPeriodID', $fil['PMBPeriodID']);
			
			//$this->db->where('a.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
}

?>